import scrapy
import logging


class WeatherSpider(scrapy.Spider):
    name = 'weather_spider'
    allowed_domains = ['tianqihoubao.com']

    # 只生成2020年1月到2024年11月的URL
    start_urls = []
    for year in range(2020, 2025):
        for month in range(1, 13):
            if year == 2024 and month > 11:  # 只爬取到2024年11月
                break
            start_urls.append(f'http://www.tianqihoubao.com/lishi/leshan/month/{year}{month:02d}.html')

    def parse(self, response):
        """解析网页内容"""
        table_rows = response.xpath('//table[@class="b"]//tr')[1:]  # 跳过表头行

        for row in table_rows:
            try:
                # 提取每列数据
                date = row.xpath('.//td[1]/text()').get(default='').strip()
                weather = row.xpath('.//td[2]/text()').get(default='').strip()
                temperature = row.xpath('.//td[3]/text()').get(default='').strip()

                # 分割气温为高低温
                temp_split = temperature.replace('℃', '').split('/')
                high_temp = temp_split[0].strip() if len(temp_split) > 0 else 'N/A'
                low_temp = temp_split[1].strip() if len(temp_split) > 1 else 'N/A'

                yield {
                    'Date': date,
                    'Weather': weather,
                    'High Temp (℃)': high_temp,
                    'Low Temp (℃)': low_temp
                }
            except Exception as e:
                logging.warning(f"数据解析失败：{e}")
                continue
